package com.example.library;

import android.content.Context;
import android.graphics.Color;
import android.support.v4.view.ViewPager;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by gaoyeping on 2017/5/9.
 */

public class XPExpressionView extends RelativeLayout implements ViewPager.OnPageChangeListener {
    private Context mContext;
    private ArrayList<ImageView> dots;
    private int oldPosition;
    private List<View> datas;
    private ViewPager video_face_view_pager;

    public XPExpressionView(Context context) {
        super(context);
        this.mContext = context;
        LayoutInflater inflater = LayoutInflater.from(mContext);
        View view = inflater.inflate(R.layout.xp_expression_view_layout, this, true);
        initFaceView(view);
    }

    public XPExpressionView(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.mContext = context;
    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
    }

    private void initFaceView(View view) {
        video_face_view_pager = (ViewPager) view.findViewById(R.id.xp_expression_view_pager);
        LinearLayout dots_layout = (LinearLayout) view.findViewById(R.id.xp_expression_dots_layout);
        video_face_view_pager.addOnPageChangeListener(this);
        datas = new ArrayList<>();
        datas.add(new FaceView1(mContext));
        datas.add(new FaceView2(mContext));
        datas.add(new FaceView3(mContext));
        FaceViewPagerAdapter adapter = new FaceViewPagerAdapter(datas);
        video_face_view_pager.setAdapter(adapter);
        dots = new ArrayList<ImageView>();
        for (int i = 0; i < datas.size(); i++) {
            ImageView imageView = new ImageView(mContext);
            imageView.setPadding(8, 0, 0, 0);
            dots.add(imageView);
            dots_layout.addView(imageView);
            if (i == 0) {
                imageView.setImageResource(R.drawable.grey_point);
            } else {
                imageView.setImageResource(R.drawable.white_point);
            }
        }
    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageSelected(int position) {
        dots.get(position).setImageResource(R.drawable.grey_point);
        dots.get(oldPosition).setImageResource(R.drawable.white_point);
        oldPosition = position;
    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }
}
